﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommerceOne.Common;
using CommerceOne.Domains.Commons;
using CommerceOne.Domains.IRepositories;
using CommerceOne.Domains.SqlRepositories;
using CommerceOne.Models;
using CommerceOne.Models.Entities;
using CommerceOne.Widgets.Breadcrumb;
using CommerceOne.Widgets.Paging;
using CommerceOne.Widgets.ProductGrid;
using CommerceOne.Widgets.Sorting;

namespace CommerceOne.Controllers
{
    public class ProductController : ControllerBase
    {
        private readonly IProductRepository _productRepository = new SqlProductRepository();

        /// <summary>
        /// Indexes the specified object id.
        /// </summary>
        /// <param name="objectId">The object id.</param>
        /// <param name="page">The page.</param>
        /// <param name="objectTitle">The object title.</param>
        /// <returns></returns>
        public ActionResult Index(int objectId, int page, string objectTitle)
        {
            var viewModel = new ProductListViewModel();
            var parameter = new Parameters {ObjectId = objectId, ObjectTitle = objectTitle, PageType = PageType.ProductList, CurrentPage = page, PageSize = 6};
            this.CommonInitialize(viewModel, parameter);

            var sortingController = new SortingController {HttpContextBase = this.HttpContext};
            viewModel.SortingViewModel = sortingController.GetViewModel(parameter);

            var productGridController = new ProductGridController();
            viewModel.ProductGridViewModel = productGridController.GetViewModel(objectId, viewModel.SortingViewModel. CurrentSelectedType.Value, page, 6);
            parameter.TotalRecord = viewModel.ProductGridViewModel.TotalRecord;

            var pagingController = new PagingController {HttpContextBase = this.HttpContext};
            viewModel.PagingViewModel = pagingController.GetViewModel(parameter);

            var breadcrumbController = new BreadcrumbController {HttpContextBase = this.HttpContext};
            viewModel.Breadcrumb = breadcrumbController.GetViewModel(parameter);

            //_productRepository.GetProducts()

            //Widgets.Sorting.Configurations config = new Configurations();
            //config.SortingItems = new List<SortingItem>{ new SortingItem{No=1, IsDefault = true, SortingType = ProductSortType.NameAsc, Name = "Product Name: A to Z"},
            //new SortingItem{No=1, IsDefault = true, SortingType = ProductSortType.NameDesc, Name = "Product Name: Z to A"},
            //new SortingItem{No=1, IsDefault = true, SortingType = ProductSortType.PriceAsc, Name = "Price: lowest first"},
            //new SortingItem{No=1, IsDefault = true, SortingType = ProductSortType.PriceDesc, Name = "Price: highest first"},
            //new SortingItem{No=1, IsDefault = true, SortingType = ProductSortType.InStockFirst, Name = "Instock first"}};
            //SerializeService.Instance.SerializeObject<Widgets.Sorting.Configurations>(HttpContext.Server.MapPath(@"~/Configurations/ProductSorting.xml"), config);

            //Widgets.Breadcrumb.Configurations config = new Widgets.Breadcrumb.Configurations{Breadcrums = new List<BreadcrumbItems>()};
            //BreadcrumbItems item = new BreadcrumbItems{BreadcrumbItem = new List<BreadcrumbItem>(), PageType = PageType.ProductList};
            //item.BreadcrumbItem.Add(new BreadcrumbItem{No = 1, Content = "Home", Clickable = true, Url = "/"});
            //item.BreadcrumbItem.Add(new BreadcrumbItem { No = 2, Content = "{Category}", Clickable = true });
            //config.Breadcrums.Add(item);
            //SerializeService.Instance.SerializeObject<Widgets.Breadcrumb.Configurations>(HttpContext.Server.MapPath(@"~/Configurations/Breadcrumb.xml"), config);
            


            return View(viewModel);
        }

        /// <summary>
        /// Details the specified product id.
        /// </summary>
        /// <param name="objectId">The object id.</param>
        /// <param name="objectTitle">The object title.</param>
        /// <returns></returns>
        public ActionResult Detail(int objectId, string objectTitle)
        {
            var viewModel = new ProductDetailViewModel();
            var parameter = new Parameters { ObjectId = objectId, PageType = PageType.ProductDetail };
            this.CommonInitialize(viewModel, parameter);

            viewModel.Product = this._productRepository.GetProductById(objectId);
            foreach (var photoItem in viewModel.Product.ProductPhoto)
            {
                if (!string.IsNullOrEmpty(photoItem.Home)) photoItem.Home = DisplayUtil.GetImageUrl(photoItem.Home);
                if (!string.IsNullOrEmpty(photoItem.Large)) photoItem.Large = DisplayUtil.GetImageUrl(photoItem.Large);
                if (!string.IsNullOrEmpty(photoItem.Medium)) photoItem.Medium = DisplayUtil.GetImageUrl(photoItem.Medium);
                if (!string.IsNullOrEmpty(photoItem.ThickBox)) photoItem.ThickBox = DisplayUtil.GetImageUrl(photoItem.ThickBox);
            }

            if (viewModel.Product == null) throw new Exception("The product isnot existed.");

            // Get short content
            if (!String.IsNullOrEmpty(viewModel.Product.Description) && viewModel.Product.Description.Length > 300)
                viewModel.Product.ShortContent = viewModel.Product.Description.Substring(0, 300) + "...";
            else
            {
                viewModel.Product.ShortContent = viewModel.Product.Description;
            }

            var breadcrumbController = new BreadcrumbController {HttpContextBase = this.HttpContext};
            viewModel.Breadcrumb = breadcrumbController.GetViewModel(parameter);

            //viewModel.Product = this._productRepository.GetProduct(objectId);
            //ProductCommentViewModel productCommentViewModel = new ProductCommentViewModel();
            //productCommentViewModel.ProductId = viewModel.Product.Id;

            //int pageSize = 12;
            //int page = 1;
            //int total = this._commentRepository.Total(objectId);

            //PagingSortingNavController pagingController = new PagingSortingNavController();
            //productCommentViewModel.PagingSortingViewModel = pagingController.GetViewModel(total, pageSize, page, "/1/Product/{0}");
            //productCommentViewModel.Comments = this._commentRepository.GetComment(objectId, page, pageSize);
            //viewModel.ProductComment = productCommentViewModel;)

            return View(viewModel);
            //return View();
        }

        /// <summary>
        /// Details the specified product id.
        /// </summary>
        /// <param name="objectId">The object id.</param>
        /// <param name="contents">The contents.</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public PartialViewResult Detail(long objectId, string contents)
        {
        //    Comment comment = new Comment();
        //    comment.ProductId = objectId;
        //    comment.Content = contents;
        //    comment.UserId = 5;
        //    comment.Title = "default title";
        //    comment.Date = DateTime.Now;
        //    this._commentRepository.AddComment(comment);

        //    ProductCommentViewModel productCommentViewModel = new ProductCommentViewModel();
        //    productCommentViewModel.ProductId = comment.ProductId;

        //    int pageSize = 12;
        //    int page = 1;
        //    int total = this._commentRepository.Total(comment.ProductId);

        //    PagingSortingNavController pagingController = new PagingSortingNavController();
        //    productCommentViewModel.PagingSortingViewModel = pagingController.GetViewModel(total, pageSize, page, "/1/Product/{0}");
        //    productCommentViewModel.Comments = this._commentRepository.GetComment(comment.ProductId, page, pageSize);

        //    return PartialView("_Comments", productCommentViewModel);
        //}

        //public PartialViewResult Comments(long objectId, int page)
        //{
        //    ProductCommentViewModel productCommentViewModel = new ProductCommentViewModel();
        //    productCommentViewModel.ProductId = objectId;

        //    int pageSize = 12;
        //    int total = this._commentRepository.Total(objectId);

        //    PagingSortingNavController pagingController = new PagingSortingNavController();
        //    productCommentViewModel.PagingSortingViewModel = pagingController.GetViewModel(total, pageSize, page, "/1/Product/{0}");
        //    productCommentViewModel.Comments = this._commentRepository.GetComment(objectId, page, pageSize);

        //    return PartialView("_Comments", productCommentViewModel);

            return new PartialViewResult();
        }
    }
} 
